package org.rescue.analysis;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.tree.DefaultMutableTreeNode;

import org.rescue.Dag.Concept;
import org.rescue.Dag.DirectedAcyclicGraph;
import org.rescue.analysis.InterCollection.CCandidateData;
import org.rescue.analysis.InterCollection.GreedyCAssoc;
import org.rescue.analysis.InterCollection.GreedyCDiff;
import org.rescue.analysis.InterDimension.DCandidateData;
import org.rescue.analysis.InterDimension.GreedyDAssoc;
import org.rescue.analysis.InterDimension.GreedyDDiff;
import org.rescue.analysis.summarize.SCandidateData;
import org.rescue.analysis.summarize.GreedySummarizor;
import org.rescue.analysis.taxonomy.CollCompareSubTaxonomyDB;
import org.rescue.analysis.taxonomy.SummarizeSubTaxonomyDB;
import org.rescue.analysis.taxonomy.TestTaxonomy;

public class run {
	public static void main(String[] args) {
		DefaultMutableTreeNode testTaxonomy;
		DefaultMutableTreeNode result = null;
			
		//	parse arguments		
		if (args[0].equals("summarize")) {
			/*
			System.out.println("Loading sub-taxonomy....");
			SummarizeSubTaxonomyDB taxonomyReader = new SummarizeSubTaxonomyDB();
			String annot_table = "test_annotation1";
			String tax_table = "test_concept";
			int tax_id = 110;
			String closure_table = "test_taxonomy"; 
		
			testTaxonomy = taxonomyReader.readTaxonomy(annot_table, tax_table, tax_id, closure_table);
			taxonomyReader.printTaxonomy(testTaxonomy);
			*/
			
			DirectedAcyclicGraph tax;   
			TestTaxonomy tTax = new TestTaxonomy();
			tax = tTax.load('s');
			Vector<Concept>  r = tax.getAllRoots();			
			// for (int i = 0; i < r.size(); i++)
				Concept rootCon = r.firstElement(); 
			int totalcard = 67;
				
			System.out.println("Performing Analysis....");
			GreedySummarizor summarizor = new GreedySummarizor();			
			result = summarizor.summarize (tax,rootCon, totalcard, 4);
			
			System.out.println("ANSWERS:");
			Enumeration e = result.breadthFirstEnumeration();
			while (e.hasMoreElements()) {
				DefaultMutableTreeNode candNode = (DefaultMutableTreeNode)e.nextElement();
				SCandidateData candNodeData = (SCandidateData)candNode.getUserObject();
				System.out.println(candNodeData.getId() + " dCard: " + candNodeData.getDCard() + " " + candNodeData.getInfoLoss());
			}			
		}
		else if (args[0].equals("c-diff")) {	
			/*
			System.out.println("Loading sub-taxonomy....");
			CollCompareSubTaxonomyDB taxonomyReader = new CollCompareSubTaxonomyDB();
			String annot_table1 = "test_annotation1";
			String annot_table2 = "test_annotation";
			String tax_table = "test_concept";
			int tax_id = 110;
			String closure_table = "test_closure"; 
		
			testTaxonomy = taxonomyReader.readTaxonomy(annot_table1, annot_table2, tax_table, tax_id, closure_table, 'd');
			taxonomyReader.printTaxonomy(testTaxonomy);
			*/
			DirectedAcyclicGraph tax;   
			TestTaxonomy tTax = new TestTaxonomy();
			tax = tTax.load('c');
			Vector<Concept>  r = tax.getAllRoots();
			// for (int i = 0; i < r.size(); i++)
				Concept rootCon = r.firstElement(); 
			int totalcard1 = 39;
			int totalcard2 = 40;
			
			System.out.println("Performing GreedyCDiff Analysis....");
			
			GreedyCDiff greedyCDiff = new GreedyCDiff();
			result = greedyCDiff.compare (tax,rootCon, totalcard1, totalcard2, 2);
			
			System.out.println("ANSWERS:");
			Enumeration e = result.breadthFirstEnumeration();
			while (e.hasMoreElements()) {
				DefaultMutableTreeNode candNode = (DefaultMutableTreeNode)e.nextElement();
				CCandidateData candNodeData = (CCandidateData)candNode.getUserObject();
				if (candNodeData.isCand)
					System.out.println(candNodeData.id + " dCard1: " + candNodeData.dCard1);
			}			
		}
		else if (args[0].equals("c-assoc")) {
			/*
			System.out.println("Loading sub-taxonomy....");
			CollCompareSubTaxonomyDB taxonomyReader = new CollCompareSubTaxonomyDB();
			String annot_table1 = "test_annotation1";
			String annot_table2 = "test_annotation";
			String tax_table = "test_concept";
			int tax_id = 110;
			String closure_table = "test_closure"; 
		
			testTaxonomy = taxonomyReader.readTaxonomy(annot_table1, annot_table2, tax_table, tax_id, closure_table, 'a');
			taxonomyReader.printTaxonomy(testTaxonomy);

			 */
			DirectedAcyclicGraph tax;   
			TestTaxonomy tTax = new TestTaxonomy();
			tax = tTax.load('c');
			Vector<Concept>  r = tax.getAllRoots();
			// for (int i = 0; i < r.size(); i++)
				Concept rootCon = r.firstElement(); 
			int totalcard1 = 39;
			int totalcard2 = 40;
			
			System.out.println("Performing GreedyCAssoc Analysis....");
			
			GreedyCAssoc greedyCAssoc = new GreedyCAssoc();
			result = greedyCAssoc.compare (tax,rootCon, totalcard1, totalcard2, 2);
			
			System.out.println("ANSWERS:");
			Enumeration e = result.breadthFirstEnumeration();
			while (e.hasMoreElements()) {
				DefaultMutableTreeNode candNode = (DefaultMutableTreeNode)e.nextElement();
				CCandidateData candNodeData = (CCandidateData)candNode.getUserObject();
				if (candNodeData.isCand)
					System.out.println(candNodeData.id + " dCard1: " + candNodeData.dCard1);
			}			
		}
		else if (args[0].equals("d-assoc")) {
			/*
			System.out.println("Loading sub-taxonomy....");
			CollCompareSubTaxonomyDB taxonomyReader = new CollCompareSubTaxonomyDB();
			String annot_table1 = "test_annotation1";
			String annot_table2 = "test_annotation";
			String tax_table = "test_concept";
			int tax_id = 110;
			String closure_table = "test_closure"; 
		
			testTaxonomy = taxonomyReader.readTaxonomy(annot_table1, annot_table2, tax_table, tax_id, closure_table, 'a');
			taxonomyReader.printTaxonomy(testTaxonomy);

			 */
			DirectedAcyclicGraph tax;
			HashMap <Integer, Integer> bgConcepts = new HashMap <Integer, Integer>();
			TestTaxonomy tTax = new TestTaxonomy();
			//tax = tTax.load('d');
			tax = tTax.loadForDimCompare(bgConcepts);
			Vector<Concept>  r = tax.getAllRoots();
			Concept rootCon = r.firstElement(); 
			int totalcard = 100;
			//int totalcard2 = 40;
			
			System.out.println("Performing GreedyDAssoc Analysis....");
			
			GreedyDAssoc greedyDAssoc = new GreedyDAssoc();
			result = greedyDAssoc.compare (tax,rootCon, bgConcepts, totalcard, 2);
			
			System.out.println("ANSWERS:");
			Enumeration e = result.breadthFirstEnumeration();
			while (e.hasMoreElements()) {
				DefaultMutableTreeNode candNode = (DefaultMutableTreeNode)e.nextElement();
				DCandidateData candNodeData = (DCandidateData)candNode.getUserObject();
				if (candNodeData.isCand)
					System.out.println(candNodeData.id + " dCard: " + candNodeData.dCard);
			}		
		}
		else if (args[0].equals("d-diff")) {
			/*
			System.out.println("Loading sub-taxonomy....");
			CollCompareSubTaxonomyDB taxonomyReader = new CollCompareSubTaxonomyDB();
			String annot_table1 = "test_annotation1";
			String annot_table2 = "test_annotation";
			String tax_table = "test_concept";
			int tax_id = 110;
			String closure_table = "test_closure"; 
		
			testTaxonomy = taxonomyReader.readTaxonomy(annot_table1, annot_table2, tax_table, tax_id, closure_table, 'a');
			taxonomyReader.printTaxonomy(testTaxonomy);

			 */
			DirectedAcyclicGraph tax;
			HashMap <Integer, Integer> bgConcepts = new HashMap <Integer, Integer>();
			TestTaxonomy tTax = new TestTaxonomy();
			//tax = tTax.load('d');
			tax = tTax.loadForDimCompare(bgConcepts);
			Vector<Concept>  r = tax.getAllRoots();
			Concept rootCon = r.firstElement(); 
			int totalcard = 100;
			//int totalcard2 = 40;
			
			System.out.println("Performing GreedyDDiff Analysis....");
			
			GreedyDDiff greedyDDiff = new GreedyDDiff();
			result = greedyDDiff.compare (tax,rootCon, bgConcepts, totalcard, 2);
			
			System.out.println("ANSWERS:");
			Enumeration e = result.breadthFirstEnumeration();
			while (e.hasMoreElements()) {
				DefaultMutableTreeNode candNode = (DefaultMutableTreeNode)e.nextElement();
				DCandidateData candNodeData = (DCandidateData)candNode.getUserObject();
				if (candNodeData.isCand)
					System.out.println(candNodeData.id + " dCard: " + candNodeData.dCard);
			}		
		}				
		else {
			System.err.println("Wrong command.");
		}		
	}	
}
